﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using IService.Data.IManager;

namespace IService.Data
{
	public static class DTVData
	{
		private static Dictionary<Type, object> s_classInstances = new Dictionary<Type, object>();

		private static IUserDataManager s_usermanager = null;
		public static IUserDataManager UserManager
		{
			get
			{
				if (s_usermanager == null)
				{
					s_usermanager = (IUserDataManager)s_classInstances[typeof(IUserDataManager)];
				}
				return s_usermanager;
			}
		}

		private static INewsDaManager s_newsmanager = null;
		public static INewsDaManager NewsManager
		{
			get
			{
				if (s_newsmanager == null)
				{
					s_newsmanager = (INewsDaManager)s_classInstances[typeof(INewsDaManager)];
				}
				return s_newsmanager;
			}
		}

		private static ITypeDaManager s_typemanager = null;
		public static ITypeDaManager TypeManager
		{
			get
			{
				if (s_typemanager == null)
				{
					s_typemanager = (ITypeDaManager)s_classInstances[typeof(ITypeDaManager)];
				}
				return s_typemanager;
			}
		}

		private static IAdminDaManager s_adminmanager = null;
		public static IAdminDaManager AdminManager
		{
			get
			{
				if (s_adminmanager == null)
				{
					s_adminmanager = (IAdminDaManager)s_classInstances[typeof(IAdminDaManager)];
				}
				return s_adminmanager;
			}
		}

        private static ISupplyPurchaseDaManager s_supplyPurchasemanager = null;
        public static ISupplyPurchaseDaManager SupplyPurchaseManager
        {
            get
            {
                if (s_supplyPurchasemanager == null)
                {
                    s_supplyPurchasemanager = (ISupplyPurchaseDaManager)s_classInstances[typeof(ISupplyPurchaseDaManager)];
                }
                return s_supplyPurchasemanager;
            }
        }

        private static IActivityDaManager s_activitymanager = null;
        public static IActivityDaManager ActivityManager
        {
            get
            {
                if (s_activitymanager == null)
                {
                    s_activitymanager = (IActivityDaManager)s_classInstances[typeof(IActivityDaManager)];
                }
                return s_activitymanager;
            }
        }

        private static IActivityApplyDaManager s_activityApplymanager = null;
        public static IActivityApplyDaManager ActivityApplyManager
        {
            get
            {
                if (s_activityApplymanager == null)
                {
                    s_activityApplymanager = (IActivityApplyDaManager)s_classInstances[typeof(IActivityApplyDaManager)];
                }
                return s_activityApplymanager;
            }
        }

        private static IActivityOpinionDaManager s_activityOpinionmanager = null;
        public static IActivityOpinionDaManager ActivityOpinionManager
        {
            get
            {
                if (s_activityOpinionmanager == null)
                {
                    s_activityOpinionmanager = (IActivityOpinionDaManager)s_classInstances[typeof(IActivityOpinionDaManager)];
                }
                return s_activityOpinionmanager;
            }
        }

        private static IProductsDaManager s_productsmanager = null;
        public static IProductsDaManager ProductsManager
        {
            get
            {
                if (s_productsmanager == null)
                {
                    s_productsmanager = (IProductsDaManager)s_classInstances[typeof(IProductsDaManager)];
                }
                return s_productsmanager;
            }
        }

		public static void Initialize(string binPath)
		{
			try
			{
				XmlDocument xmlDoc = new XmlDocument();
				XmlNode nodeServices = null;

				Environment.CurrentDirectory = binPath;
				xmlDoc.Load(Path.Combine(binPath, "DataServices.config"));
				nodeServices = xmlDoc.SelectSingleNode("/configuration/services[1]");

				if (nodeServices != null)
				{
					string assembly = nodeServices.Attributes["assembly"].Value;

					foreach (XmlNode nodeService in nodeServices.ChildNodes)
					{
						string typeName = nodeService.Attributes["interface"].Value;
						Type type = Type.GetType(typeName);
						string implementation = nodeService.Attributes["implementation"].Value;
						object instance = Activator.CreateInstance(assembly, implementation).Unwrap();

						if (type == null)
						{
							throw new Exception("Can't retrieve the type - " + typeName);
						}
						s_classInstances.Add(type, instance);
					}
				}
			}
			catch (Exception ex)
			{
				throw new Exception("Error occurred in instantiating Manager classes.", ex);
			}
		}

	}
}
